srcdir = @srcdir@
prefix	= @prefix@
exec_prefix = @exec_prefix@
sbindir	= @sbindir@
mandir	= @mandir@
libdir  = @libdir@
etcdir	= /etc
vardir	= /var/lib/isns
systemddir = $(prefix)/lib/systemd/system
datarootdir = @datarootdir@
includedir = @includedir@

VPATH = $(srcdir)
SBINDIR	= $(DESTDIR)$(sbindir)
CFGDIR	= $(DESTDIR)$(etcdir)/isns
MANDIR	= $(DESTDIR)$(mandir)
VARDIR	= $(DESTDIR)$(vardir)
SYSTEMDDIR = $(DESTDIR)$(systemddir)
LIBDIR = $(DESTDIR)$(libdir)
INCDIR = $(DESTDIR)$(includedir)/libisns
PKGDIR = $(LIBDIR)/pkgconfig

ENABLE_SHARED = @ENABLE_SHARED@
ENABLE_STATIC = @ENABLE_STATIC@
HAVE_LD_VERSION_SCRIPT = @HAVE_LD_VERSION_SCRIPT@
ifneq ($(ENABLE_SHARED),1)
BUILD_STATIC  = 1
else
BUILD_STATIC  = $(ENABLE_STATIC)
endif
ifeq ($(HAVE_LD_VERSION_SCRIPT),1)
SOLIB_VERSION_OPTS = -Wl,--version-script=$(srcdir)/libisns.vers
else
SOLIB_VERSION_OPTS =
endif

CC	= @CC@
CPPFLAGS= @CPPFLAGS@
CFLAGS	= @CFLAGS@ -I. -Iinclude -I$(srcdir) -I$(srcdir)/include
LDFLAGS	= @LDFLAGS@
INSTALL = @INSTALL@

LIB	= libisns.a
SOLIB   = libisns.so.0
SOLIB_SONAME = libisns.so.0
LIBOBJS	= server.o \
	  client.o \
	  objects.o \
	  callback.o \
	  timer.o \
	  vendor.o \
	  db.o \
	  db-file.o \
	  db-policy.o \
	  relation.o \
	  scope.o \
	  message.o \
	  security.o \
	  authblock.o \
	  policy.o \
	  pki.o \
	  register.o \
	  query.o \
	  getnext.o \
	  deregister.o \
	  esi.o \
	  scn.o \
	  dd.o \
	  entity.o \
	  portal-group.o \
	  storage-node.o \
	  domain.o \
	  simple.o \
	  tags.o \
	  attrs.o \
	  export.o \
	  socket.o \
	  slp.o \
	  error.o \
	  logging.o \
	  config.o \
	  parser.o \
	  buffer.o \
	  pidfile.o \
	  sysdep-unix.o \
	  util.o \
	  bitvector.o \
	  mdebug.o
SOLIBOBJS = $(patsubst %.o,solib-%.o,$(LIBOBJS))
HDRS    = $(srcdir)/include/libisns/attrs.h \
	  $(srcdir)/include/libisns/buffer.h \
	  $(srcdir)/include/libisns/isns.h \
	  $(srcdir)/include/libisns/isns-proto.h \
	  $(srcdir)/include/libisns/message.h \
	  include/libisns/paths.h \
	  $(srcdir)/include/libisns/source.h \
	  $(srcdir)/include/libisns/types.h \
	  $(srcdir)/include/libisns/util.h

SECLINK	= @SECLIBS@
SLPLINK	= @SLPLIBS@
SLPLIN	= @SLPLIBS@
ADMOBJS = isnsadm.o
SRVOBJS = isnsd.o
ISNSDD	= local.o isnsdd.o

TESTS	= tests/pauw1 tests/pauw2 tests/pauw3 tests/pauw4

all: $(LIB) $(SOLIB) isnsd isnsadm isnsdd $(TESTS)

install: all
	@echo "*** Installing Open-iSNS ***"
	$(INSTALL) -m 755 -d $(CFGDIR) $(MANDIR)/man8 $(MANDIR)/man5 $(SBINDIR) $(SYSTEMDDIR) $(PKGDIR)
	$(INSTALL) -m 700 -d $(VARDIR)
	$(INSTALL) -m 555 isnsd isnsadm isnsdd $(SBINDIR)
	$(INSTALL) -m 644 $(srcdir)/etc/isnsd.conf $(CFGDIR)
	$(INSTALL) -m 644 $(srcdir)/etc/isnsdd.conf $(CFGDIR)
	$(INSTALL) -m 644 $(srcdir)/etc/isnsadm.conf $(CFGDIR)
	$(INSTALL) -m 644 $(srcdir)/doc/isnsd.8 $(MANDIR)/man8
	$(INSTALL) -m 644 $(srcdir)/doc/isnsdd.8 $(MANDIR)/man8
	$(INSTALL) -m 644 $(srcdir)/doc/isnsadm.8 $(MANDIR)/man8
	$(INSTALL) -m 644 $(srcdir)/doc/isnssetup.8 $(MANDIR)/man8
	$(INSTALL) -m 644 $(srcdir)/doc/isns_config.5 $(MANDIR)/man5
	$(INSTALL) -m 644 $(srcdir)/isnsd.service $(SYSTEMDDIR)
	$(INSTALL) -m 644 $(srcdir)/isnsd.socket $(SYSTEMDDIR)
	$(INSTALL) -m 644 $(srcdir)/libisns.pc $(PKGDIR)

install_hdrs: 
	@echo '*** Installing Open-iSNS header files ***'
	$(INSTALL) -m 755 -d $(INCDIR)
	$(INSTALL) -m 644 $(HDRS) $(INCDIR)

install_lib: $(LIB)
	@echo '*** Installing Open-iSNS developer files ***'
	$(INSTALL) -m 755 -d $(LIBDIR)
ifeq ($(ENABLE_SHARED),1)
	$(INSTALL) -m 644 $(SOLIB) $(LIBDIR)
	ln -sf $(SOLIB) $(LIBDIR)/libisns.so
endif
ifeq ($(ENABLE_STATIC),1)
	$(INSTALL) -m 644 $(LIB) $(LIBDIR)
endif

clean distclean::
	rm -f *.o $(LIB) $(SOLIB) libisns.so isnsd isnsadm isnsdd bitvector *~
	$(RM) $(TESTS)

distclean::
	rm -f config.h Makefile config.status config.log include/libisns/paths.h
	rm -rf autom4te.cache

%.o: %.c
	@mkdir -p $(@D)
	$(CC) $(CFLAGS) $(CPPFLAGS) -c -o $@ $^

solib-%.o: %.c
	$(CC) $(CFLAGS) $(CPPFLAGS) -fPIC -c -o $@ $<

message.o: message.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(IQNFLAGS) -c -o $@ $<

solib-message.o: message.c
	$(CC) $(CFLAGS) $(CPPFLAGS) $(IQNFLAGS) -fPIC -c -o $@ $<

ifeq ($(BUILD_STATIC),1)
$(LIB): $(LIBOBJS)
	ar cr $@ $(LIBOBJS)
else
$(LIB):
endif

ifeq ($(ENABLE_SHARED),1)
$(SOLIB): $(SOLIBOBJS)
	$(CC) $(CFLAGS) $(LDFLAGS) -fPIC -shared -Wl,-soname,$(SOLIB_SONAME) $(SOLIB_VERSION_OPTS) -o $@ $(SOLIBOBJS) -Wl,--as-needed $(SECLINK) $(SLPLINK)
	[ -L libisns.so ] || ln -s $(SOLIB) libisns.so
else
$(SOLIB):
	touch $(SOLIB)
endif

isnsd: $(SRVOBJS) $(LIB) $(SOLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(SRVOBJS) -L. -lisns -Wl,--as-needed $(SECLINK) $(SLPLINK)

isnsdd: $(ISNSDD) $(LIB) $(SOLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(ISNSDD) -L. -lisns -Wl,--as-needed $(SECLINK) $(SLPLINK)

isnsadm: $(ADMOBJS) $(LIB) $(SOLIB)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(ADMOBJS) -L. -lisns -Wl,--as-needed $(SECLINK) $(SLPLINK)

tests/%: tests/%.o $(LIB) $(SOLIB)
	@mkdir -p $(@D)
	$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $@.o -L. -lisns -Wl,--as-needed $(SECLINK) $(SLPLINK)

bitvector: bitvector.c $(LIB) $(SOLIB)
	$(CC) -DTEST $(CFLAGS) $(LDFLAGS) -o $@ bitvector.c -L. -lisns

depend:
	$(CC) $(CFLAGS) -M `ls *.c` > .depend

-include .depend
